package com.ch.clinking.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ch.clinking.ClinkingApplication;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.DoCallBack;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.adapter.ProfitListViewAdapter;
import com.ch.clinking.adapter.SaleListViewAdapter;
import com.ch.clinking.bean.MerSaleDTO;
import com.ch.clinking.databinding.ActivityCalculateProfitBinding;
import com.ch.clinking.databinding.ActivityCalculateProfitListBinding;
import com.google.gson.Gson;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class CalculateProfitListActivity extends AppCompatActivity {

    private ActivityCalculateProfitListBinding binding;

    private ProfitListViewAdapter profitListViewAdapter;

    private List<MerSaleDTO> merSaleDotList;

    private ClinkingApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityCalculateProfitListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        app = ClinkingApplication.getInstance();

        // 设置状态栏图标为深色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        refreshProfitList();


    }

    public void refreshProfitList() {
        showLoading();

        HashMap<String, String> selectMerchandiseList = new HashMap<>();
        selectMerchandiseList.put("shopId", app.shopList.get(app.shopPosition).getShopId());

        OkHttpUtil.getInstance().doPostForm("/mer/getShopProfitInfoByFromDataBase", new DoCallBack() {
            @Override
            public void onSuccess(String result) {
                MerSaleDTO[] merSaleDTOArray = new Gson().fromJson(result, MerSaleDTO[].class);
                merSaleDotList = Arrays.asList(merSaleDTOArray);
                binding.autoCalculateProfitMerList.setLayoutManager(new LinearLayoutManager(CalculateProfitListActivity.this));
                profitListViewAdapter = new ProfitListViewAdapter(CalculateProfitListActivity.this, merSaleDotList, binding.autoCalculateProfitLoadingProgressBar);
                binding.autoCalculateProfitMerList.setAdapter(profitListViewAdapter);

                hideLoading();

            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(CalculateProfitListActivity.this, "网络错误,获取商品失败,请稍后再试", Toast.LENGTH_SHORT).show();
                hideLoading();
            }
        }, selectMerchandiseList);
    }

    private void showLoading() {
        binding.autoCalculateProfitLoadingProgressBar.setVisibility(View.VISIBLE);
        // 禁用用户交互
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }

    private void hideLoading() {
        binding.autoCalculateProfitLoadingProgressBar.setVisibility(View.GONE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }


}